/*
 * This program creates an application demonstrating validation of DOM using Validation APIs.
 * DOMValidator.java
 */
package Aptech.JavaXML.DOMValidator;

import java.text.DecimalFormat;
import java.util.Scanner;
/*
 * @author Vincent
 * Copyright � 2007 Aptech Software Limited. All rights reserved.
 * The DOMValidator validates the XML document against the specified XML by first creating a DOM source for the document.
 */

public class DOMValidator {

    static Scanner input = new Scanner(System.in);
    static CreateSurvey create = new CreateSurvey();
    static ReadSurvey read = new ReadSurvey();
    static CreateQuestionAndAnswer createQA = new CreateQuestionAndAnswer();
    static ReadQuestionAndAnswer qAndA = new ReadQuestionAndAnswer();

    private static void displayQA() {
        int i = 0;
        for (Question question : qAndA.readQuestion("question.xml")) {
            i = 0;
            System.out.println(question.toString());
            for (Answer answer : qAndA.readAnswer("question.xml", question.getId())) {
                i++;
                double rate = 0;
                if(Integer.parseInt(answer.getRate())>0)
                {
                    rate = Double.parseDouble(answer.getRate())*100/qAndA.numberManTl;
                }
                DecimalFormat df = new DecimalFormat("#.##");
                System.out.println(i + answer.toString()+"\t"+df.format(rate)+"%");
            }
            qAndA.setNumberManTl(0);
        }
    }

    public static int chooseMenu() {
        try {
            String[] items = {"Create Servey", "Create Question", "Create Answer", "Display Question", "Remove answer","Remove Question","Remove Survey", "Exit"};
            System.out.println("=== DOM Demo ===");
            for (int i = 0; i < items.length; i++) {
                System.out.println(String.valueOf(i + 1) + ". " + items[i]);
            }
            System.out.print("Choose : ");
            int choose = Integer.parseInt(input.nextLine());
            return choose;
        } catch (Exception ex) {
            return -1;
        }
    }

    public static void main(String[] args) {
        try {
            org.w3c.dom.Document doc=null;
            String filename =null;
            while(true){
                int choose = chooseMenu();
                switch(choose){
                    case 1:
                        updateSurvey();
                    break;
                    case 2:
                        updateQuestion();
                    break;
                    case 3:
                        updateAnswer();
                    break;
                    case 4:
                        displayQA();
                    break;
                    case 5:
                        System.out.println("Enter id");
                        String id = input.nextLine();
                        createQA.removeAnser(id);
                        createQA.save();
                    break;
                    case 6:
                        System.out.println("Enter id");
                        String qId = input.nextLine();
                        createQA.removeQuestion(qId);
                        createQA.save();
                    break;
                    case 7:
                        System.out.println("Enter id");
                        String sId = input.nextLine();
                        create.removeSurvey(sId);
                        create.save();
                    break;
                    case 8:
                        System.exit(0);
                    break;
                }
            }
            
        } catch (Exception ex) {
            System.out.println("document is INvalid!" + ex.getMessage());
        }
    }


    

    public static void updateSurvey() {
        try {
            Survey s = new Survey();
            System.out.println("Enter Id");
            s.setIdSurvey(input.nextLine());
            System.out.println("Enter subject");
            s.setContentSurvey(input.nextLine());
            s.setCommenSurvey("");
            create.create(s);
            create.save();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public static void updateQuestion() {
        try {
            Question q = new Question();
            System.out.println("Enter Id");
            q.setId(input.nextLine());
            System.out.println("Enter content");
            q.setContent(input.nextLine());
            System.out.println("Enter id servey");
            q.setCaId(input.nextLine());
            createQA.createQuestion(q);
            createQA.save();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void updateAnswer() {
        try {

            Answer s = new Answer();
            System.out.println("Enter Id");
            s.setId(input.nextLine());
            System.out.println("Enter option");
            s.setOption(input.nextLine());
            System.out.println("Enter Id question");
            s.setQuesId(input.nextLine());
            createQA.createAnswer(s);
            createQA.save();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
